﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using sixgod.Base.Controllers;
using sixgod.Dtos.WorkFlow;
using sixgod.Dtos.WorkFlow.Search;
using sixgod.Service.WorkFlowTemplate;

namespace sixgod.Module.WorkFlow.Controllers
{
    [Route("[Controller]")]
    public class WorkFlowTemplateController:BaseController
    {
        private IWorkFlowTemplateService _workFlowTemplateService;
        private IMapper _mapper;

        public WorkFlowTemplateController(IWorkFlowTemplateService workFlowTemplateService, IMapper mapper)
        {
            _workFlowTemplateService = workFlowTemplateService;
            _mapper = mapper;
        }

        [HttpGet("page")]
        public async Task<IActionResult> GetPageList([FromQuery]WorkFlowTemplateQuery query)
        {
            var res = await _workFlowTemplateService.GetPageList(query);
            return Success(res);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> Get(string id)
        {
            var model = await _workFlowTemplateService.GetByIdAsync(id);
            return Success(_mapper.Map<WorkFlowTemplateOutDto>(model));
        }

        [HttpGet("select")]
        public async Task<IActionResult> GetTemplateList()
        {
            var list = await _workFlowTemplateService.GetTemplateList();
            return Success(list);
        }

        [HttpPost]
        public async Task<IActionResult> NewTemplate([FromBody]WorkFlowTemplateInputDto data)
        {
            try
            {
                await _workFlowTemplateService.InsertWorkTemplate(data);
                return Success("ok");
            }
            catch (Exception e)
            {
                return Fail(e.Message);
            }
        }

        [HttpDelete("{Id}")]
        public async Task<IActionResult> DeleteTemplate(string Id)
        {
            await _workFlowTemplateService.DeleteTemplate(Id);
            return Success("success");
        }


        #region formDesign

        /// <summary>
        /// 新增流程表单设计
        /// </summary>
        /// <returns></returns>
        [HttpPost("formDesign")]
        public async Task<IActionResult> InsertTemplateFromDesign(FormDesignInDto modelDto)
        {
            modelDto.FormItems.ForEach(r=>
            {
                r.Create("admin");
                r.TemplateId = modelDto.WorkFlowId;
            });
            await _workFlowTemplateService.InsertFormItem(modelDto.FormItems);
            return Success("OK");
        }

        [HttpGet("formItems/{workFlowId}")]
        public async Task<IActionResult> GetTemplateFormItems(string workFlowId)
        {
            var list = await _workFlowTemplateService.GetTemplateFormItems(workFlowId);
            return Success(list);
        }

        #endregion

        #region FlowDesign

        [HttpGet("flowDesign/{workFlowId}")]
        public async Task<IActionResult> GetTemplateFlow(string workFlowId)
        {
            var data = await _workFlowTemplateService.GetFlowDesignDto(workFlowId);
            return Success(data);
        }

        [HttpPost("flowDesign")]
        public async Task<IActionResult> InsertFlowDesign(FlowDesignInDto data)
        {
            await _workFlowTemplateService.InsertFlowDesign(data);
            return Success(123);
        }

        #endregion
    }
}
